package com.nf.teamwork.mall.config.security.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.nf.teamwork.mall.vo.ResponseVO;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: LJP
 * @Classname JwtAuthFailHandler
 * @Date: 2020-03-01 19:14
 * @Description: 认证失败处理类
 */
public class JwtAuthFailHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        ResponseVO vo = new ResponseVO();
        vo.setCode(httpServletResponse.SC_INTERNAL_SERVER_ERROR);
        vo.setMessage("login fail");
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writeValue(httpServletResponse.getOutputStream(), vo);
    }
}
